import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/user/user.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cates from '../components/goods/cate.vue'
import Params from '../components/goods/params.vue'
import Lists from '../components/goods/list.vue'
import Adds from '../components/goods/add.vue'
import Orders from '../components/order.vue'
import Reports from '../components/reports.vue'

Vue.use(Router)

const router = new Router({
        routes: [
            { path: '/', redirect: '/login' },
            { path: '/login', component: Login },
            {
                path: '/home',
                component: Home,
                redirect: '/welcome',
                children: [
                    { path: '/welcome', component: Welcome },
                    { path: '/users', component: User },
                    { path: '/rights', component: Rights },
                    { path: '/roles', component: Roles },
                    { path: '/categories', component: Cates },
                    { path: '/params', component: Params },
                    { path: '/goods', component: Lists },
                    { path: '/goods/add', component: Adds },
                    { path: '/orders', component: Orders },
                    { path: '/reports', component: Reports }
                ]
            }
        ]
    })
    // 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

export default router